package com.xdxc.utils;

public class ExcelColumnConverter {

    /**
     * 将数字转换为Excel列标格式（1→A, 26→Z, 27→AA）
     * @param number 要转换的正整数
     * @return Excel列标字符串
     */
    public static String convertToExcelColumn(int number) {
        if (number <= 0) throw new IllegalArgumentException("输入必须为正整数");

        StringBuilder sb = new StringBuilder();
        while (number > 0) {
            int remainder = number % 26;
            number = number / 26;
            if (remainder == 0) {
                remainder = 26;
                number--; // 处理余数为0的特殊情况
            }
            sb.insert(0, (char) ('A' + remainder - 1));
        }
        return sb.toString();
    }

    // 使用示例
    public static void main(String[] args) {
        System.out.println(convertToExcelColumn(1));    // A
        System.out.println(convertToExcelColumn(26));   // Z
        System.out.println(convertToExcelColumn(27));   // AA
        System.out.println(convertToExcelColumn(702));  // ZZ
    }
}